/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package expenseslist;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.freixas.jcalendar.JCalendarCombo;

/**
 *
 * @author trunghpb
 */
public class AddExpensesFrame extends JFrame {
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 200;
    
    public AddExpensesFrame(ExpensesFrame parent) throws Exception {
        setTitle("Expense Frame");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        
        _parent = parent;
        
        expense = new ExpenseDB();        
        
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5,2, 5, 5));
        
        addTextBox(expensesNameTextField, panel, "Name");
        addTextBox(expensesDescriptionTextField, panel, "Description");
        
        panel.add(new JLabel("Date", SwingConstants.RIGHT));
        calendar = new JCalendarCombo(JCalendarCombo.DISPLAY_DATE, false);
        panel.add(calendar);

        addTextBox(expensesAmounthTextField, panel, "Amounth");        
        addComboBox(expensesAccountComboBox, panel, "Account");
        
        AccountDB accountDb = new AccountDB();
        DbAdapter adap = new DbAdapter();
        adap.addString("account_name");
        adap.addInt("account_id");
        accountList = adap.getArray(accountDb.find());
        
        for (int i = 0; i<accountList.length; i++){
            expensesAccountComboBox.addItem(accountList[i][0].toString());
        }
        add(panel, BorderLayout.CENTER);
        
        JPanel buttonPanel = new JPanel();
        addButton(buttonPanel, "Insert", new AddExpenseListener());
        addButton(buttonPanel, "Cancel", new ExitListener());

        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    public void addButton(JPanel panel, String lable, ActionListener listener){
        JButton button = new JButton(lable);
        panel.add(button);
        button.addActionListener(listener);
    }
    
    public void addTextBox(JTextField textField, JPanel panel, String label){
        JLabel jlabel = new JLabel(label, SwingConstants.RIGHT);
        panel.add(jlabel);
        panel.add(textField);
    }
    
    public void addComboBox(JComboBox comboBox, JPanel panel, String label){
        JLabel jlabel = new JLabel(label, SwingConstants.RIGHT);
        panel.add(jlabel);
        panel.add(comboBox);
    }

    private ExpenseDB expense;

    private class AddExpenseListener implements ActionListener{

        public void actionPerformed(ActionEvent ae) {
            try {
                expense.setName(expensesNameTextField.getText());
                expense.setAmounth(Integer.parseInt(expensesAmounthTextField.getText()));
                expense.setDesc(expensesDescriptionTextField.getText());
                int selected = expensesAccountComboBox.getSelectedIndex();
                
                AccountDB account = new AccountDB(Integer.parseInt(accountList[selected][1].toString()));
                expense.setAccount(account);
                
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                expense.setDate(sdf.format(calendar.getCalendar().getTime()));
                
                if (expense.insert() ==  ExpenseDB.SUCCESS){
                    try {
                        _parent.reloadExpenseList();
                    } catch (Exception ex) {
                        Logger.getLogger(AddExpensesFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    dispose();
                    
                }else{
                    JOptionPane.showMessageDialog(AddExpensesFrame.this, "Can not insert data to database");      
                }
            } catch (SQLException ex) {
                Logger.getLogger(AddExpensesFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    private class ExitListener implements ActionListener{

        public void actionPerformed(ActionEvent ae) {
            dispose();
        }
    }

    JTextField expensesNameTextField = new JTextField();
    JTextField expensesDescriptionTextField = new JTextField();
    JTextField expensesDateTextField = new JTextField();
    JTextField expensesAmounthTextField = new JTextField();
    JComboBox expensesAccountComboBox = new JComboBox();
    JCalendarCombo calendar;
    Object[][] accountList;
    ExpensesFrame _parent;
    
}
